package cn.bytecollege.admin.filter; /**
 * @Auther：MR.P
 * @Date:2021/10/28 16 19
 * @Description:对添加支付方式1和修改的请求进行过滤
 */

import cn.bytecollege.common.entity.Payment;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "PaymentFilter",servletNames = {"PaymentSaveController","PaymentUpdateController"})
public class PaymentFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        String method = req.getMethod();

        if(method.equalsIgnoreCase("post")){
            String paymentName1 = req.getParameter("paymentName");
            if(paymentName1==null||paymentName1.equals("")){
                String msg = "请填写支付方式名称！";
                request.setAttribute("msg",msg);
                if(uri.contains("paymentSave")){
                    request.getRequestDispatcher("/admin/html/device/paymentSave.jsp").forward(request,response);
                    return;
                }else if(uri.contains("paymentUpdate")){
                    String id = req.getParameter("paymentId");
                    Integer paymentId = Integer.valueOf(id);
                    String paymentName = req.getParameter("paymentName");
                    Payment tempPayment = new Payment();
                    tempPayment.setPaymentName(paymentName);
                    tempPayment.setPaymentId(paymentId);
                    //把原来的数据返回去
                    request.setAttribute("payment",tempPayment);
                    request.getRequestDispatcher("/admin/html/device/paymentUpdate.jsp").forward(request,response);
                    return;
                }
                return;
            }
            //如果支付方式名称不为null也不为空，则放行
            chain.doFilter(request, response);
            return;
        }
        chain.doFilter(request, response);
    }
}
